package com.lyy.mapper;

import com.lyy.entity.Clazz;
import com.lyy.entity.Student;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Repository
public interface ClazzMapper {
    String selectClazzList = "select c.clazz_id, c.clazz_name, c.create_time, c.modify_time from springboot.clazz c";

    @Select(selectClazzList)
    List<Clazz> selectClazzList();

    String insertClazz ="insert into springboot.clazz(clazz_id, clazz_name, create_time, modify_time) value (#{clazzId},#{clazzName},#{createTime},#{modifyTime})";
    @Options(useGeneratedKeys = true,keyProperty = "clazzId")
    @Insert(insertClazz)
    int insertClazz(Clazz clazz);

    String updateByClazzId="<script>" +
            "update springboot.clazz " +
            "<set>" +
            "<if test='clazzName !=null'> clazz_name=#{clazzName}, </if> " +
            "<if test='createTime !=null'> create_time=#{createTime}, </if> " +
            "<if test='modifyTime !=null'> modify_time=#{modifyTime}, </if> " +
            "</set>" +
            "where clazz_id=#{clazzId}" +
            "</script>";


    @Update(updateByClazzId)
    int updateByClazzId(Clazz clazzId);

    String deleteByClazzId="delete from springboot.clazz where clazz_id=#{clazzId}";
    @Delete(deleteByClazzId)
    int deleteByClazzId(Clazz clazz);


    String clazz_list= "select c.clazz_id, c.clazz_name, c.create_time, c.modify_time from springboot.clazz c";
    @Select(clazz_list + " where clazz_id = #{param1}")
    Clazz selectByClazzId(Integer clazzId);


    @Results({
            @Result(id = true, property = "clazzId", column = "clazz_id"),
            @Result(property = "student", column = "clazz_id",
                    many = @Many(select = "com.lyy.mapper.StudentMapper.selectByStudentId"))
    })
    @Select(clazz_list)
    List<Clazz> listDetail();
}
